package com.keon.jianbao.ui.fragment;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.keon.jianbao.AppContext;
import com.keon.jianbao.R;
import com.keon.jianbao.api.Api;
import com.keon.jianbao.api.SimpleHandler;
import com.keon.jianbao.base.BaseActivity;
import com.keon.jianbao.base.BaseFragment;
import com.keon.jianbao.bean.Result;
import com.keon.jianbao.bean.User;
import com.keon.jianbao.enu.ListType;
import com.keon.jianbao.enu.WebType;
import com.keon.jianbao.ui.activity.ListActivity;
import com.keon.jianbao.ui.activity.MoreActivity;
import com.keon.jianbao.ui.activity.MyInfoActivity;
import com.keon.jianbao.ui.activity.MyTweetActivity;
import com.keon.jianbao.ui.activity.TweetPublishActivity;
import com.keon.jianbao.ui.activity.WebActivity;
import com.keon.jianbao.ui.widget.CircleImageView;
import com.keon.jianbao.ui.widget.ItemLayout;
import com.keon.jianbao.util.JsonUtils;
import com.keon.jianbao.util.StringUtils;
import com.keon.jianbao.util.UILUtils;
import com.nostra13.universalimageloader.core.ImageLoader;

import org.apache.http.Header;
import org.json.JSONException;

import butterknife.Bind;

/**
 * Created by keon on 2015/9/8.
 */
public class MyFragemnt extends BaseFragment {

    @Bind(R.id.my_more)
    ItemLayout ilMore;
    @Bind(R.id.my_message)
    ItemLayout ilMessage;
    @Bind(R.id.my_jianbao)
    ItemLayout ilJianBao;
    @Bind(R.id.my_ticket)
    ItemLayout ilTicket;
    @Bind(R.id.my_activity)
    ItemLayout ilActivity;
    @Bind(R.id.my_info_layout)
    RelativeLayout rlInfo;
    @Bind(R.id.my_name)
    TextView tvName;
    @Bind(R.id.my_avatar)
    CircleImageView sdvAvatar;
    @Bind(R.id.my_tweet)
    ItemLayout ilTweet;

    SimpleHandler userInfoHandler = new SimpleHandler() {
        @Override
        public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
            super.onSuccess(statusCode, headers, responseBody);
            Result result = null;
            try {
                result = new Result(responseBody);
                if (result.getStatus() > 0) {
//                    TLog.log(result.getContent() + result.getTips());
//                    LoginActivity.showToast(result.getTips());
                    User user = JsonUtils.fromJson(result.getContent(), User.class);
                    AppContext.saveUserInfo(user);

                    String name = AppContext.getInstance().getProperty("user.user_name");
                    String face = AppContext.getInstance().getProperty("user.user_face");
                    String gender = AppContext.getInstance().getProperty("user.user_gender");
                    if (!StringUtils.isEmpty(name)) {
                        tvName.setText(name);
                    }
                    if (!StringUtils.isEmpty(face)) {
//                        sdvAvatar.setImageURI(Uri.parse(face));
                        ImageLoader.getInstance().displayImage(face, sdvAvatar, UILUtils.getOptions1());
                    }
                    Drawable drawable = StringUtils.getGendorDrawable(gender);
                    tvName.setCompoundDrawables(null, null, drawable, null);
                } else {
//                    LoginActivity.showToast(result.getTips());
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my;
    }

    @Override
    public void initView(View view) {
        ilMore.setOnClickListener(this);
        ilMessage.setOnClickListener(this);
        ilJianBao.setOnClickListener(this);
        ilTicket.setOnClickListener(this);
        ilActivity.setOnClickListener(this);
        rlInfo.setOnClickListener(this);
        ilTweet.setOnClickListener(this);
    }

    @Override
    public void initData() {
        userInfoHandler.setContext((BaseActivity) getActivity());
/*        String name = AppContext.getInstance().getProperty("user.user_name");
        String face = AppContext.getInstance().getProperty("user.user_face");
        if (!StringUtils.isEmpty(name)) {
            tvName.setText(name);
        }
        if (!StringUtils.isEmpty(face)) {
            sdvAvatar.setImageURI(Uri.parse(face));
        }*/
    }

    @Override
    public void onClick(View view) {
        BaseActivity ba = (BaseActivity) getActivity();
        Bundle bundle = new Bundle();
        int id = view.getId();
        switch (id) {
            case R.id.my_more:
                ba.jump(MoreActivity.class);
                break;
            case R.id.my_message:
                bundle.putInt("type", ListType.MESSAGELIST.getId());
                ba.jump(ListActivity.class, bundle);
                break;
            case R.id.my_jianbao:
                bundle.putInt("type", ListType.JIANBAOLIST.getId());
                ba.jump(ListActivity.class, bundle);
                break;
            case R.id.my_ticket:
                bundle.putInt("type", ListType.ORDERLIST.getId());
                ba.jump(ListActivity.class, bundle);
                break;
            case R.id.my_activity:
                bundle.putInt("type", WebType.ACTIVITY_SELF_SHOW.getId());
                ba.jump(WebActivity.class, bundle);
                break;
            case R.id.my_info_layout:
                ba.jump(MyInfoActivity.class);
                break;
            case R.id.my_tweet:
                ba.jump(MyTweetActivity.class);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        ((BaseActivity) getActivity()).showWaitDialog();
        Api.user_info(AppContext.getInstance().getProperty("user.code"), userInfoHandler);
    }
}
